import 'dart:convert';
import 'dart:ui';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:panda/base/SignType.dart';
import 'package:panda/http/Api.dart';
import 'package:panda/http/HttpUtil.dart';
import 'package:panda/util/SignUtil.dart';

import '../base/Constant.dart';

class EveryDayStarPage extends StatefulWidget {
  EveryDayStarPage({Key key}) : super(key: key);

  @override
  _EveryDayStarPageState createState() => _EveryDayStarPageState();
}

class _EveryDayStarPageState extends State<EveryDayStarPage> {
  EasyRefreshController _controller = new EasyRefreshController();
  List<String> names = new List();
  List<String> avatars = new List();
  List<String> nums = new List();
  var time1 = "日排行帮", time2 = "周排行榜", time3 = "月排行榜", current = "日排行帮";
  @override
  void initState() {
    super.initState();
    loadList();
  }

  loadList() async {
    Map map = new Map();
    map['type'] =
        current.contains("日") ? "1" : current.contains("周") ? "2" : "3";
    map['sign_type'] = SignType.GET_RANK_EVERYDAY;
    Response<String> response =
        await HttpUtil().post(Api.GET_RANK_ERERYDAY, data: getSign(map));
    Map userMap = json.decode(response.data);
    if (1 == userMap['code'] && mounted) {
      names.clear();
      avatars.clear();
      nums.clear();
      (userMap['data'] as List).forEach((v) {
        names.add(v['nickname'] == null ? "" : v['nickname']);
        nums.add(v['num']);
        avatars.add(v['avatar'] == null
            ? ""
            : v['avatar'].toString().replaceAll("\\", ""));
      });
      setState(() {
        
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        physics: new NeverScrollableScrollPhysics(),
        child: Stack(
          children: <Widget>[
            Image.asset(
              "images/bg_ranking.png",
              fit: BoxFit.fill,
              width: MediaQuery.of(context).size.width,
            ),
            Container(
              width: double.infinity,
              height: MediaQuery.of(context).size.height -
                  MediaQueryData.fromWindow(window).padding.top,
              margin: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_20),
                  MediaQueryData.fromWindow(window).padding.top,
                  ScreenUtil.getInstance().setHeight(DP_20),
                  0),
              child: new Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  GestureDetector(
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                    child: Icon(
                      Icons.arrow_back_ios,
                      color: white,
                      size: ScreenUtil.getInstance().setHeight(DP_40),
                    ),
                  ),
                  Container(
                    height: ScreenUtil.getInstance().setHeight(DP_450 - DP_80),
                  ),
                  Container(
                    height: MediaQuery.of(context).size.height -
                        MediaQueryData.fromWindow(window).padding.top -
                        ScreenUtil.getInstance().setHeight(DP_80) -
                        ScreenUtil.getInstance().setHeight(DP_450 - DP_80),
                    padding: EdgeInsets.all(
                        ScreenUtil.getInstance().setHeight(DP_20)),
                    decoration: BoxDecoration(
                        color: white, borderRadius: BorderRadius.circular(10)),
                    child: Column(
                      children: <Widget>[
                        new Row(
                          children: <Widget>[
                            Expanded(
                              child: GestureDetector(
                                behavior: HitTestBehavior.opaque,
                                onTap: () {
                                    current = time1;
                                    loadList();
                                },
                                child: Center(
                                  child: Column(
                                    children: <Widget>[
                                      Text(time1,
                                          style: TextStyle(
                                              color: black_99,
                                              fontSize: ScreenUtil.getInstance()
                                                  .setSp(time1 == current
                                                      ? NORMAL_FONT_SIZE
                                                      : SMALL_FONT_SIZE))),
                                      Offstage(
                                        offstage: time1 != current,
                                        child: Container(
                                          margin: EdgeInsets.all(
                                              ScreenUtil.getInstance()
                                                  .setHeight(DP_10)),
                                          width: DP_20,
                                          height: 2,
                                          color: time1 == current
                                              ? mainColor
                                              : Colors.transparent,
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                            ),
                            Container(
                                width: 2,
                                height:
                                    ScreenUtil.getInstance().setHeight(DP_20),
                                color: Colors.black45),
                            Expanded(
                              child: GestureDetector(
                                behavior: HitTestBehavior.opaque,
                                onTap: () {
                                    current = time2;
                                    loadList();
                                },
                                child: Center(
                                  child: Column(
                                    children: <Widget>[
                                      Text(time2,
                                          style: TextStyle(
                                              color: black_99,
                                              fontSize: ScreenUtil.getInstance()
                                                  .setSp(time2 == current
                                                      ? NORMAL_FONT_SIZE
                                                      : SMALL_FONT_SIZE))),
                                      Offstage(
                                        offstage: time2 != current,
                                        child: Container(
                                          margin: EdgeInsets.all(
                                              ScreenUtil.getInstance()
                                                  .setHeight(DP_10)),
                                          width: DP_20,
                                          height: 2,
                                          color: time2 == current
                                              ? mainColor
                                              : Colors.transparent,
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                            ),
                            Container(
                                width: 2,
                                height:
                                    ScreenUtil.getInstance().setHeight(DP_20),
                                color: Colors.black45),
                            Expanded(
                              child: GestureDetector(
                                behavior: HitTestBehavior.opaque,
                                onTap: () {
                                    current = time3;
                                    loadList();
                                },
                                child: Center(
                                  child: Column(
                                    children: <Widget>[
                                      Text(time3,
                                          style: TextStyle(
                                              color: black_99,
                                              fontSize: ScreenUtil.getInstance()
                                                  .setSp(time3 == current
                                                      ? NORMAL_FONT_SIZE
                                                      : SMALL_FONT_SIZE))),
                                      Offstage(
                                        offstage: time3 != current,
                                        child: Container(
                                          margin: EdgeInsets.all(
                                              ScreenUtil.getInstance()
                                                  .setHeight(DP_10)),
                                          width: DP_20,
                                          height: 2,
                                          color: time3 == current
                                              ? mainColor
                                              : Colors.transparent,
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                        Expanded(
                          child: ListView.builder(
                            padding: EdgeInsets.only(top: 0),
                            shrinkWrap: true,
                            itemCount: names.length,
                            itemBuilder: (BuildContext context, int index) {
                              return renderRow(context, index);
                            },
                          ),
                        )
                      ],
                    ),
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  renderRow(BuildContext context, int index) {
    return Container(
      margin: EdgeInsets.fromLTRB(
          ScreenUtil.getInstance().setHeight(0),
          ScreenUtil.getInstance().setHeight(DP_20),
          ScreenUtil.getInstance().setHeight(0),
          0),
      padding: EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_20)),
      child: Row(
        children: <Widget>[
          Text((index + 1).toString(),
              style: TextStyle(
                  color: index == 0
                      ? mainColor
                      : index == 1 ? Colors.deepOrange : black_99,
                  fontSize: index == 0
                      ? ScreenUtil.getInstance().setSp(BIG_FONT_SIZE * 2 - 15)
                      : index == 1
                          ? ScreenUtil.getInstance()
                              .setSp(NORMAL_FONT_SIZE * 2 - 15)
                          : ScreenUtil.getInstance().setSp(BIG_FONT_SIZE))),
          Container(
            width: DP_20,
          ),
          ClipOval(
            child: avatars[index] == ""||avatars[index] == null
                ? Image.asset('images/app_icon.png',
                    width: ScreenUtil.getInstance().setHeight(DP_60),
                    height: ScreenUtil.getInstance().setHeight(DP_60))
                : CachedNetworkImage(
                imageUrl: avatars[index].contains("http")? avatars[index]:Api.ImgBaseUrl + avatars[index],
                    width: ScreenUtil.getInstance().setHeight(DP_60),
                    fit: BoxFit.cover,
                    height: ScreenUtil.getInstance().setHeight(DP_60)),
          ),
          Container(
            width: DP_15,
          ),
          Text(names[index],
              style: TextStyle(
                  color: index == 0
                      ? mainColor
                      : index == 1 ? Colors.deepOrange : black_99,
                  fontSize: index == 0
                      ? ScreenUtil.getInstance().setSp(BIG_FONT_SIZE)
                      : index == 1
                          ? ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)
                          : ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE))),
          Expanded(
            child: Center(),
          ),
          Text(nums[index],
              style: TextStyle(
                  color: mainColor,
                  fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE))),
          Container(
            width: 5,
          ),
          Text("单",
              style: TextStyle(
                  color: black_99,
                  fontSize: ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE))),
        ],
      ),
    );
  }
}
